package com.sqstudio.model
{
	import com.sqstudio.common.json.JSON;
	import com.sqstudio.event.CalendarEvent;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	 *数据中心 
	 * @author Nestor
	 * 
	 */	
	public class Model
	{
		
		public var star_obj:Object,huilv_obj:Object,history_obj:Object,time_obj:Object;
		private var callBackFun:Function = null;
		private var crtParseFun:Function = null;
		private var parseNum:int=0;
		//应用中 的 公共日期
		public static var CrtDate:Date = new Date();
		
		private static var _instance:Model = null; 
		public function Model(p:Param)
		{
			
		}
		/**
		 *获得数据单例 
		 * @return 
		 * 
		 */		
		public static function getInstance():Model{
			if(_instance == null){
				_instance = new Model(new Param());
			}
			return _instance;
		}
		public function init():void{
			remote(JSON.HUILV,this.rmbParse);
			remote(JSON.STAR,this.starParse);
			remote(JSON.HISTORY,this.historyParse);
			remote(JSON.TIME,this.timeParse);
		}
		
		/**
		 *远程请求 
		 * @param $url
		 * 
		 */		
		private function remote($url:String,$parseFun:Function):void{
			var urlLaoder:URLLoader = new URLLoader();
			urlLaoder.addEventListener(Event.COMPLETE,$parseFun);
			urlLaoder.addEventListener(IOErrorEvent.IO_ERROR,errorHadnler);
			urlLaoder.load(new URLRequest($url+"?cache="+int(Math.random()*1000)));
		}
//		/**
//		 *天气数据解析 
//		 * @param e
//		 * 
//		 */		
		private function weatherParse():void{
//			this.parseNum++;
//			if(this.parseNum>=4){
//				CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.UPDATE_WEATHER));
//			}
		
		}
		/**
		 *汇率数据解析 
		 * @param e
		 * 
		 */		
		private function rmbParse(e:Event):void{
			removeListener(e);
			trace(URLLoader(e.currentTarget).data);
			this.huilv_obj = JSON.decode(URLLoader(e.currentTarget).data);
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.UPDATE_RMB));
			weatherParse();
			
			
		}
		/**
		 *星座数据解析 
		 * @param e
		 * 
		 */		
		private function starParse(e:Event):void{
			removeListener(e);
			trace(URLLoader(e.currentTarget).data);
			this.star_obj = JSON.decode(URLLoader(e.currentTarget).data);
			star_obj["aries"].id=1;
			star_obj["taurus"].id=2;
			star_obj["gemini"].id=3;
			star_obj["cancer"].id=4;
			star_obj["leo"].id=5;
			star_obj["virgo"].id=6;
			star_obj["libra"].id=7;
			star_obj["scorpio"].id=8;
			star_obj["sagittarius"].id=9;
			star_obj["capricorn"].id=10;
			star_obj["aquarius"].id=11;
			star_obj["pisces"].id=12;
			
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.UPDATE_STAR));
			weatherParse();
		}
		
		/**
		 *历史今天数据解析 
		 * @param e
		 * 
		 */		
		private function historyParse(e:Event):void{
			removeListener(e);
			trace(URLLoader(e.currentTarget).data);
			this.history_obj = JSON.decode(URLLoader(e.currentTarget).data);
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.UPDATE_HISTORY));
			weatherParse();
		}
		/**
		 *北京时间解析 
		 * @param e
		 * 
		 */		
		private function timeParse(e:Event):void{
			removeListener(e);
			trace(URLLoader(e.currentTarget).data);
			this.time_obj = JSON.decode(URLLoader(e.currentTarget).data);
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.UPDATE_TIME));
			weatherParse();
		}
		
		
		
		/**
		 *错误处理 
		 * @param e
		 * 
		 */		
		private function errorHadnler(e:IOErrorEvent):void
		{
			removeListener(e);
			trace("加载数据出错",e.text);
		}
		/**
		 *移除侦听 
		 * @param e
		 * 
		 */		
		private function removeListener(e:Event):void{
			var urlLaoder:URLLoader = e.currentTarget as URLLoader;
			urlLaoder.removeEventListener(IOErrorEvent.IO_ERROR,errorHadnler);
		}
	}
}
class Param{}
	